package com.pet.server.service.impl;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.pet.server.domain.entity.Goods;
import com.pet.server.mapper.GoodsMapper;
import com.pet.server.service.IGoodsService;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class GoodsServiceImpl implements IGoodsService {
    private final GoodsMapper goodsMapper;

    @Override
    public boolean addNewGoods(Goods goods) {
        return goodsMapper.insertOrUpdate(goods) > 1;
    }

    @Override
    public boolean delGoodsById(int goodsId) {
        return goodsMapper.deleteById(goodsId) > 0;
    }

    @Override
    public Page<Goods> findGoodsListByName(int pageNo, int pageSize, int shopId, String goodsName) {
        QueryWrapper<Goods> qw = new QueryWrapper<>();

        qw.eq("fk_shop_id", shopId);
        qw.like("goods_name", goodsName);

        Page<Goods> list = goodsMapper.selectPage(new Page<>(pageNo, pageSize), qw);

        return list;
    }

    @Override
    public boolean updateGoodsSelective(Goods goods) {
        return goodsMapper.insertOrUpdateSelective(goods) > 0;
    }

    @Override
    public Page<Goods> getAllGoodsList(int pageNo, int pageSize, int shopId) {
        QueryWrapper<Goods> qw = new QueryWrapper<>();

        qw.eq("fk_shop_id", shopId);

        Page<Goods> list = goodsMapper.selectPage(new Page<>(pageNo, pageSize), qw);

        return list;
    }

    @Override
    public Page<Goods> getGoodsListByGoodsType(int pageNo, int pageSize, int shopId, String goodsType) {
        QueryWrapper<Goods> qw = new QueryWrapper<>();

        qw.eq("fk_shop_id", shopId);
        qw.like("goods_type", goodsType);

        return goodsMapper.selectPage(new Page<>(pageNo, pageSize), qw);
    }

    @Override
    public Goods getGoodsSelectiveById(int id) {
        return goodsMapper.selectById(id);
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
